package com.Classroom.al.controllers;


import com.Classroom.al.entities.Student;
import com.Classroom.al.services.StudenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("students")
public class StudentController {

    @Autowired
    StudenService studenService;

    @GetMapping
    public String displayStudents(Model model){
        List<Student> students=studenService.getAll();
        model.addAttribute("students",students);
        return "students/list-students";
    }
    @GetMapping("/new")
    public String displayStudentForm(Model model){

        Student aStudent=new Student();
        model.addAttribute("student",aStudent);
        return "students/new-student";
    }
    @PostMapping("/save")
    public String createStudent(Model model,Student student){

        studenService.save(student);

        return "redirect:/students";
    }
    @RequestMapping("/del/{id}")
    public String dStudent(@PathVariable("id") long id){
        System.out.println(id);
        studenService.del(id);
        return "redirect:/students";
    }
}
